home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / TextHarvest / TextHarvest-Install.exe / {app} / Help-Decapsulators.txt < prev    next >
Text File  |  2005-01-04  |  19KB  |  331 lines

  1. <h1>Parse-O-Matic: Decapsulators</h1>
  2.  
  3. <hl>Note: This is an appendix to the "Parse-O-Matic Scripts" user manual.</hl>
  4.  
  5. <h2>Table of Contents</h2>
  6.  
  7. You can click on any section title below to jump directly to that section.
  8.  
  9.    <a href="#INTRODUC">Introduction</a>
  10.       <a href="#QUICKREF">Quick Reference</a>
  11.       <a href="#SIMPLEEX">A Simple Example</a>
  12.    <a href="#YDECAPNE">Why Decapsulators are Necessary</a>
  13.    <a href="#OCCURENC">Introduction to Occurrence Numbers</a>
  14.       <a href="#SAMPLEAP">Sample Application</a>
  15.       <a href="#MAOCCURN">Occurrence Number Syntax</a>
  16.       <a href="#FTFALOCC">Finding the First and Last Occurrence</a>
  17.       <a href="#FNEXTOCC">Finding the Next Occurrence</a>
  18.    <a href="#PLAINDEC">The Plain Decapsulator</a>
  19.    <a href="#POSITION">Positional Decapsulators</a>
  20.       <a href="#POSISAFE">Using Positional Decapsulators Safely</a>
  21.    <a href="#UNSUCCES">Unsuccessful Searches</a>
  22.    <a href="#CONTROLS">The Control Setting</a>
  23.    <a href="#NULLDECA">The Null Decapsulator</a>
  24.       <a href="#YNULLDEC">Why Null Decapsulators Work Differently</a>
  25.    <a href="#OVERLAPD">Overlapping Decapsulators<a>
  26.    <a href="#PARSEMPF">Parsing Empty Fields</a>
  27.  
  28. <a name="INTRODUC"><h2>Introduction</h2>
  29.  
  30. A "decapsulator" is a command parameter that defines a search for where a string of characters either begins or ends.
  31.  
  32. If that definition was not particularly helpful, it is because decapsulators cannot be fully described by a single sentence. But we encourage you to read through this appendix, for the following reason:
  33. <hl>
  34.   Decapsulators let a <b>single</b> Parse-O-Matic Scripting command accomplish
  35.   what might take <b>dozens</b> of commands in a standard programming language.
  36. </hl>
  37. The underlying concept is this: when analyzing data, the part you are interested in (the "field") is typically surrounded ("encapsulated") by some kind of distinctive text. A decapsulator looks for the distinctive text on either side of the data you want and thus helps you extract the field.
  38.  
  39. Sometimes the "distinctive text" appears more than once in the data you are examining. Decapsulators can handle this situation.
  40.  
  41. Sometimes one edge of the field is the beginning or end of the data you are examining, so there is no "distinctive text" to look for. Decapsulators can handle this situation, too.
  42.  
  43. <a name="QUICKREF"><h3>Quick Reference</h3>
  44.  
  45. Here are some sample decapsulators:
  46. <hl>
  47.   ùùùùùùùùù   ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù   ùùùùùùùùùùùùùùùùùùùùùùùùù
  48.   Sample      "From" Decapsulator Meaning      "To" Decapsulator Meaning
  49.   ùùùùùùùùù   ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù   ùùùùùùùùùùùùùùùùùùùùùùùùù
  50.   '23'        From column 23 onwards           Up to column 23
  51.   'AB'        After first occurrence of 'AB'   Before first 'AB'
  52.   '1*CD'      After first occurrence of 'CD'   Before first 'CD'
  53.   '5*EF'      After fifth occurrence of 'EF'   Before fifth 'EF'
  54.   '<*GH'      After first occurrence of 'GH'   Before first 'GH'
  55.   '>*IJ'      After last  occurrence of 'IJ'   Before last  'IJ'
  56.   ''          From left edge of data           From right edge of data
  57.   '-2'        Two columns in from the right    Same
  58.   ùùùùùùùùù   ùùùùùùùùùùùùùùùùùùùùùùùùùùùùùù   ùùùùùùùùùùùùùùùùùùùùùùùùù
  59. </hl>
  60. Each of these techniques is explained below in more detail.
  61.  
  62. <a name="SIMPLEEX"><h3>A Simple Example</h3>
  63.  
  64. Here is an example of how decapsulators work. Consider the following commands.
  65. <hl>
  66.   SourceVar = 'AAABBBCCC'
  67.   ResultVar = Parse SourceVar '3*A' '1*C'
  68. </hl>
  69. The second command means "Set ResultVar to everything between the third occurrence of 'A' and the first occurrence of 'C'.'  In other words, ResultVar will end up containing 'BBB'.
  70.  
  71. <a name="YDECAPNE"><h2>Why Decapsulators are Necessary</h2>
  72.  
  73. When analyzing data, the fields you are interested in are sometimes arranged in tidy columns ù but not always. Quite frequently, a field will start after some kind of delimiter, as in the following example.
  74. <hl>
  75.   SourceVar = 'Mouse,Gazelle,Mouse,Elephant'
  76. </hl>
  77. Here the fields are separated by commas ù a commonly-used format for data known as CSV (Comma Separated Values).
  78.  
  79. Extracting, say, the second item from free-form data is rather awkward if you are using a standard programming language. Fortunately, Parse-O-Matic Scripting has been developed with precisely this kind of situation in mind.
  80.  
  81. Using decapsulators, the Parse command lets you extract the "Nth" item. For example, to extract the third item in the free-form example above, you could use this command:
  82. <hl>
  83.   ResultVar  =  Parse  SourceVar  '2*,'  '3*,'
  84. </hl>
  85. This means "Set the variable ResultVar by looking in SourceVar and taking everything between the second comma and the third comma". ResultVar would thus be set to 'Mouse'.
  86.  
  87. <a name="OCCURENC"><h2>Introduction to Occurrence Numbers</h2>
  88.  
  89. Let's have another look at that last command.
  90. <hl>
  91.   ResultVar  =  Parse  SourceVar  '2*,'  '3*,'
  92. </hl>
  93. The first decapsulator (i.e. the <hl>'2*,'</hl> part) is the "From" specification. The second decapsulator (i.e. the <hl>'3*,'</hl> part) is the "To" specification. It is interpreted as follows:
  94. <hl>
  95.   3   means "the third occurrence"
  96.   *   marks the end of the occurrence number
  97.   ,   is the text you are looking for
  98. </hl>
  99. Decapsulators can be used to find more than a single character. Let's say that (for some odd reason) a variable named xyz has been set such that each field is separated with a pair of X's, as in the following example.
  100. <hl>
  101.   xyz = 'mouse<b>XX</b>gazelle<b>XX</b>mouse<b>XX</b>elephant'
  102. </hl>
  103. You can extract the third item with this command:
  104. <hl>
  105.   abc   =   Parse   xyz   '2*<b>XX</b>'   '3*<b>XX</b>'
  106.   ___               ___    _ __     _ __
  107.    |                 |     |  |     |  |
  108.    Variable to set   |     |  |     |  |
  109.     Variable to search     |  |     |  "To" text being sought
  110.     "From" occurrence number  |     "To" occurrence number
  111.        "From" text being sought
  112. </hl>
  113. This command sets the variable abc to 'mouse', since it is found between the second and third occurrences of <b>XX</b>.
  114.  
  115. <a name="SAMPLEAP"><h3>Sample Application</h3>
  116.  
  117. The Parse command is particularly useful for extracting information from CSV (Comma Separated Value) files. Here is an example of a CSV file:
  118. <hl>
  119.   "Mouse","Gazelle","Mouse","Elephant"
  120.   "Dog","Giraffe","Elk","Mongoose"
  121.   "Monkey","Snake","Caribou","Trout"
  122. </hl>
  123. These fields could be extracted with this series of commands:
  124. <hl>
  125.   field1 = Parse $Data '1*"'  '2*"'
  126.   field2 = Parse $Data '3*"'  '4*"'
  127.   field3 = Parse $Data '5*"'  '6*"'
  128.   field4 = Parse $Data '7*"'  '8*"'
  129. </hl>
  130. For the first line of the input file, field1 is set to 'Mouse', field2 is set to 'Gazelle', and so on.
  131.  
  132. <a name="MAOCCURN"><h3>Occurrence Number Syntax</h3>
  133.  
  134. Occurrence numbers must be larger than zero. The following lines are <b>not</b> valid Parse commands:
  135. <hl>
  136.   field1 = Parse $Data '0*"'  '2*"'   <ùù "From" decapsulator is zero
  137.   field2 = Parse $Data '-1*"' '2*"'   <ùù "From" decapsulator is negative
  138. </hl>
  139. The occurrence number must always be followed by an asterisk (the * character) so you can search for a number. Consider the following example (the meaning of which would be unclear without the asterisk):
  140. <hl>
  141.   MyVar  =  Parse  'xxx2yyy2zzz2'  '1*2'  '2*2'
  142. </hl>
  143. This sets MyVar to the text occurring between the first '2' and the second '2'. In other words, MyVar is set to 'yyy'.
  144.  
  145. <a name="FTFALOCC"><h3>Finding the First and Last Occurrence</h3>
  146.  
  147. A decapsulator can refer to "the <b>last</b> occurrence":
  148. <hl>
  149.   xyz  =  Parse  'AaaBAbbBAccB'  '>*A'  '>*B'
  150. </hl>
  151. In both decapsulators, the > symbol means "the last occurrence". Thus, the command means, "Set the xyz variable to everything between the last A and the last B". Thus, the xyz variable is set to "cc".
  152.  
  153. You can also use the < character to mean "the <b>first</b> occurrence", though this is somewhat redundant, since the following commands are equivalent:
  154. <hl>
  155.   abc  =  Parse  'AaaBAbbBAccB'  '<*A'  '<*B'
  156.   abc  =  Parse  'AaaBAbbBAccB'  '1*A'  '1*B'
  157.   abc  =  Parse  'AaaBAbbBAccB'  'A'    'B'
  158. </hl>
  159. All three commands would set the abc variable to 'aa'.
  160.  
  161. <a name="FNEXTOCC"><h3>Finding the Next Occurrence</h3>
  162.  
  163. When using occurrence numbers for certain kinds of data, you will often find that the "To" occurrence number is 1 (one) more than the "From" occurrence number. Consider this example:
  164. <hl>
  165.   xyz = 'AB,CD,EF,GH'
  166.   Field1 = Parse xyz '' '1*,'
  167.   Field2 = Parse xyz '1*,' '2*,'
  168.   Field3 = Parse xyz '2*,' '3*,'
  169. </hl>
  170. For Field3 you are extracting everything between the second and third comma. It can become tiresome to write code like this ù always adding one to the "From" occurrence number. Fortunately, you can use the "next occurrence" symbol '@*' in the "To" decapsulator:
  171. <hl>
  172.   xyz = 'AB,CD,EF,GH'
  173.   abc = Parse xyz '2*,' '@*,'
  174. </hl>
  175. This will set the "From" position to the second comma, and the "To" position to the comma after that (i.e. the third one). The '@*' symbol means "Look for the To text starting immediately after the From text".
  176.  
  177. Note: The "next occurrence" symbol (@*) can only be used in the "To" decapsulator.
  178.  
  179. <a name="POSITION"><h2>Positional Decapsulators</h2>
  180.  
  181. Note:  Positional decapsulators imply that operations proceed from or to the exact character position indicated, regardless of the <a href="#CONTROLS">control settings</a>.
  182.  
  183. You can specify a number to indicate the "From" or "To" position. In this mode, the Parse command behaves exactly like the Cols command. Thus, the following two commands accomplish the same thing:
  184. <hl>
  185.   xyz  =  Parse  MyVar  '10'  '20'
  186.   xyz  =  Cols   MyVar  '10'  '20'
  187. </hl>
  188. As such, this is not particularly helpful. However, you can combine positional decapsulators with other types of decapsulators, as in this example:
  189. <hl>
  190.   MyVar  =  'ABCD/abcd/'
  191.   abc    =  Parse  MyVar  '3'  '1*/'
  192. </hl>
  193. This will set the variable abc to 'CD'.
  194.  
  195. You can also count backwards from the right edge of the data. Consider this example:
  196. <hl>
  197.   MyVar  =  'ABCDEFG'
  198.   xyz    =  Parse  MyVar  '-3'  '-2'
  199. </hl>
  200. This will set the variable xyz to 'EF'. (The last character in a variable is represented by position '-1'.)
  201.  
  202. <a name="POSISAFE"><h3>Using Positional Decapsulators Safely</h2>
  203.  
  204. You need to be careful when you use positional decapsulators. If, for example, you use a negative positional decapsulator, and you end up referring to a character before the beginning of the string, it isn't clear what you "mean" by that.
  205.  
  206. For the reason just noted, and others that will become evident as you write scripts: if there is a chance that a positional decapsulator will refer to a character position of zero or less, or if it might refer to a position beyond the end of the data, you should check the length of the data before trying the command.
  207.  
  208. <a name="PLAINDEC"><h2>The Plain Decapsulator</h2>
  209.  
  210. The occurrence number is not always needed. Either the "From" or "To" decapsulator can be represented as a plain (non-numeric) string, as in the following example.
  211. <hl>
  212.   OldVar  =  'zzzABChelloXYZzzz'
  213.   NewVar  =  Parse  OldVar  'ABC'  'XYZ'
  214. </hl>
  215. This would set the variable named NewVar to 'hello' since it means:
  216. <hl>
  217.   - Copy from  the character following the first 'ABC'
  218.   - Copy up to the character preceding the first 'XYZ'
  219. </hl>
  220. This is, of course, equivalent to the following command, which uses occurrence numbers:
  221. <hl>
  222.   NewVar  =  Parse  OldVar  '1*ABC'  '1*XYZ'
  223. </hl>
  224. In general, it is best to explicitly give occurrence numbers, unless you know that the format of the data is not going to change.
  225.  
  226. <a name="UNSUCCES"><h2>Unsuccessful Searches</h2>
  227.  
  228. When a command that uses decapsulators does not find the search text, it does as little as possible. For example, if a Parse command does not find the encapsulating text, it sets the variable to a null (''). Here are two examples:
  229. <hl>
  230.   abc  =  Parse  'ABCDEFGHIJ'  '1*K'  '1*J'   <ùù There is no 'K'
  231.   abc  =  Parse  'ABCDEFGHIJ'  '1*A'  '1*X'   <ùù There is no 'X'
  232. </hl>
  233. To illustrate this principle further: if the Overlay command does not find the search text, it does nothing at all, as in the following example.
  234. <hl>
  235.   abc = 'ABCDEFGHIJ'        <ùù Set a variable
  236.   Overlay abc 'K' 'LMNOP'   <ùù There is no 'K', so nothing is done
  237. </hl>
  238. If the "From" value is less than the "To" value, the Parse-O-Matic engine will display an error message, then terminate further processing. For example:
  239. <hl>
  240.   abc  =  Parse  abc  'ABCDEFGHIJ'  '1*J'  '1*A'   <ùù 'J' comes <b>after</b> 'A'
  241. </hl>
  242. This kind of failure typically happens if the data contains an odd arrangement of text that you had not foreseen. In such case, it would not be reasonable for processing to continue; you need to be warned about departures from what your script implies you expected.
  243.  
  244. <a name="CONTROLS"><h2>The Control Setting</h2>
  245.  
  246. Commands that use decapsulators typically have a "control setting" that allows you to adjust the way the command is performed. These settings are described in more detail in the "Parse-O-Matic Scripts" user manual, but a few examples can be given here.
  247.  
  248. The Parse command's control setting tells Parse whether to include or exclude the surrounding (i.e. searched-for) text. By default, the surrounding text is excluded (unless the decapsulator is <a href="#POSITION">positional</a>). However, if you want to include it, you can add 'Include' at the end of the Parse command, as in this example:
  249. <hl>
  250.   xyz  =  Parse  'aXcaYcaZc'  '2*a'  '2*c'  'Include'
  251. </hl>
  252. This tells the command to give you everything between the second 'a' and the second 'c' ù <b>including</b> the 'a' and 'c'. In other words, this sets the variable xyz to 'aYc'.
  253.  
  254. You can also set the Control specification to 'Exclude', though since this is the default setting for Parse, it isn't necessary. Here is an example:
  255. <hl>
  256.   xyz  =  Parse  'a1ca2ca3c'  '2*a'  '2*c'  'Exclude'
  257. </hl>
  258. This sets the variable xyz to '2'.
  259.  
  260. You can specify several control settings at once, separated by spaces.  By default, the Parse command's control setting is 'Exclude MatchCase' but you could set this to (for example) 'Include IgnoreCase'.
  261.  
  262. <a name="NULLDECA"><h2>The Null Decapsulator</h2>
  263.  
  264. Here is helpful variation of the "From" decapsulator:
  265. <hl>
  266.   ''   means "Start from the first character in the value being analyzed"
  267. </hl>
  268. A similar variation can be used with the "To" decapsulator:
  269. <hl>
  270.   ''   means "End with the last character in the value being analyzed"
  271. </hl>
  272. If you use the null ('') decapsulator for "From" or "To", the "found" value (the first character for "From", or the last character for "To") will always be included (see <a href="#OVERLAPD">Overlapping Decapsulators</a> for an exception to this rule). Here is an example:
  273. <hl>
  274.   xyz  =  Parse  'ABCABCABC'  ''  '2*C"
  275. </hl>
  276. This sets the variable xyz to "ABCAB". The "From" value (i.e. the first character) is <b>not</b> excluded. However, when Parse finds the "To" value (i.e. the second occurrence of the letter C) it <b>is</b> excluded. If you want to include the second 'C', you should write the command this way:
  277. <hl>
  278.   xyz  =  Parse  'ABCABCABC'  ''  '2*C'  'Include'
  279. </hl>
  280. Incidentally, the following two commands accomplish the same thing:
  281. <hl>
  282.   xyz  =  Parse  'ABCD'  ''  ''
  283.   xyz  = 'ABCD'
  284. </hl>
  285. They are equivalent because the Parse command means "Set the variable xyz with everything between (and including) the first character and the last character".
  286.  
  287. <a name="YNULLDEC"><h3>Why Null Decapsulators Work Differently</h3>
  288.  
  289. It may not be immediately obvious why decapsulator-enabled commands treat the null ('') decapsulator differently. The examples given here are very simple, and not representative of real-world applications.
  290.  
  291. In day-to-day usage, though, you will frequently find it helpful to be able to specify a command that says, "Give me everything from the beginning of the line to just before such-and-such" or "Give me everything from such-and-such a point until the end of the line."
  292.  
  293. For example, here is a command that means "Give me everything from just after the dollar sign, to the end of the line":
  294. <hl>
  295.   xyz  =  Parse  'Please give me $199.00'  '1*$'  ''
  296. </hl>
  297. This sets xyz to "199.00". If you want to include the dollar sign, write the command this way:
  298. <hl>
  299.   xyz  =  Parse  'Please give me $199.00'  '1*$'  ''  'Include'
  300. </hl>
  301. In this example, the 'Include' control setting affects the way the "From" decapsulator works, since it is using an <a href="#OCCURENC">occurrence number</a>.
  302.  
  303. <a name="OVERLAPD"><h2>Overlapping Decapsulators</h2>
  304.  
  305. Earlier, it was mentioned that the text found by the null decapsulator is "always included" and is not affected by the 'Exclude' control setting. There is an exception to this: if the null decapsulator's "found text" is contained in the text found by the other decapsulator, it <b>can</b> be affected. For example:
  306. <hl>
  307.   xyz  =  Parse  'ABCDEFABCDEF'  ''  '1*AB'  'Exclude'
  308. </hl>
  309. This command means "Give me everything between the first character and the first occurrence of AB". Since the two items overlap (i.e. the first 'AB' includes the first character), the first character does indeed get excluded. As a result, the xyz variable is set to an empty string ('').
  310.  
  311. Here is another example:
  312. <hl>
  313.   xyz  =  Parse  'ABCDEFABCDEF'  '>*F'  ''  'Exclude'
  314. </hl>
  315. This command means "give me everything between the last occurrence of F and the last character". Both decapsulators refer to the same character (i.e. the final 'F'), so it is excluded. As a result, the xyz variable is set to an empty string ('').
  316.  
  317. Note:  In some circumstances, the FindPosn command is <b>not</b> affected by this exception. It will do its best to make sense of your request if the        decapsulators overlap, and one of them is a null decapsulator.  The "Parse-O-Matic Scripts" manual explains this in more detail.
  318.  
  319. <a name="PARSEMPF"><h2>Parsing Empty Fields</h2>
  320.  
  321. Consider the following command, which is operating on CSV (Comma Separated Value) data.
  322. <hl>
  323.   xyz  =  Parse  ',,,JOHN,SMITH'  '2*,'  '3*,'
  324. </hl>
  325. There is nothing between the second and third comma, so the xyz variable is set to '' (an empty string).
  326.  
  327. Now consider this command:
  328. <hl>
  329.   xyz  =  Parse  ',,,JOHN,SMITH'  ''  ','
  330. </hl>
  331. You are asking for everything from the first character to the first comma (which also happens to be the first character). Obviously, there is nothing "between" the two characters, so the xyz variable would be set to '' (an empty string).  This may be what you wanted, but whenever you are dealing with a field at the beginning or end of data, and there is a chance the field might be empty, it is a good idea to test your script to make sure that it does what you expect.